#include<Windows.h>
#include<stdio.h>
#include<time.h>
#include "bmp.h"
#include "yuv2rgb.h"
//Windows消息回掉
LRESULT CALLBACK WndMainProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
//注册窗口
int RegisterWinClass(const char*classname);
HWND CreateWinByClassName(const char*classname,int width,int height);
typedef struct _YUVINFO{
FILE*fp;
int width;
int height;
char**buffer;
}YUVINFO;
VOID CALLBACK YuvMainProc(HWND hWnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime){
     YUVINFO*info=(YUVINFO*)idEvent;
     if(fread(info->buffer[0],1,info->width*info->height*3/2,info->fp)!=0){
#if 1
         I420toRGBEX(info->buffer[0],info->buffer[3],info->width,info->height,24);
#else
         I420toRGB(info->buffer[0],info->buffer[3],info->width,info->height,24);
         //要交换Rb颜色
         //还要上下翻转        
         RGBChangeRBColor(info->buffer[3],info->width,info->height,24);
         RGBChangeUD(info->buffer[3],info->width,info->height,24);
#endif
         ShowBmp(hWnd,info->buffer[3],info->width,info->height,24);
      }else{
         printf("Plav End\n");
         PostMessage(hWnd,WM_QUIT,0,0);
	  }
}
//创建窗口
int PlayYUVByWin(const char*filename,int width,int height){
	MSG msg={0};
	HWND hWnd=NULL;
	const char*classname="PlayYuv";
	FILE*fp=fopen(filename,"rb");
	char*buffer[4]={NULL,NULL,NULL,NULL};
    YUVINFO yuvinfo;
	buffer[0]=(char*)malloc(width*height*3/2);
	buffer[1]=buffer[0]+width*height;
	buffer[2]=buffer[1]+width*height/4;
	buffer[3]=(char*)malloc(width*height*4);
	if(RegisterWinClass(classname)!=0){
	   return -1;
	}
	hWnd=CreateWinByClassName(classname,width,height);
	if(hWnd==NULL)return -1;
	ShowWindow(hWnd,SW_SHOWDEFAULT);
	UpdateWindow(hWnd);//更新窗口
    yuvinfo.fp=fp;
    yuvinfo.width=width;
    yuvinfo.height=height;
    yuvinfo.buffer=buffer;
    SetTimer(hWnd,(UINT_PTR)&yuvinfo,40,YuvMainProc);
	//进入消息循环
	while(1){
	  if(PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
	     if(msg.message == WM_QUIT) 
            break ; 
         TranslateMessage (&msg) ; 
         DispatchMessage (&msg) ; 
       }
    }
    KillTimer(hWnd,(UINT_PTR)&yuvinfo);
	free(buffer[0]);
	free(buffer[3]);
	fclose(fp);
	UnregisterClass(classname,NULL);
	return 0;
}
LRESULT CALLBACK WndMainProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch (message){
        case WM_CLOSE:            
            ShowWindow(hwnd, SW_HIDE);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
// Pass this message to the video window for notification of system changes
    return DefWindowProc (hwnd , message, wParam, lParam);
}
int RegisterWinClass(const char*classname){
    WNDCLASS wc;
	ZeroMemory(&wc,sizeof wc);
//	wc.style		 = WS_OVERLAPPEDWINDOW;//窗口风格
	wc.lpfnWndProc   = WndMainProc;//回调函数
    wc.hInstance     = NULL;//应用句柄
    wc.lpszClassName = classname;//注册类名
    wc.lpszMenuName  = NULL;//菜单名
//  wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);//背景色设置
//	HBITMAP hBitmap=(HBITMAP)LoadImage(0,TEXT("back.bmp"),IMAGE_BITMAP,0,0,LR_SHARED|LR_LOADFROMFILE);
//	wc.hbrBackground = CreatePatternBrush(hBitmap);//将bmp位图生成画刷,设置为背景,这里拉伸,平铺在WM_PAINT使用StretchBlt实现
//	DeleteObject (hBitmap) ;//释放bmp句柄，
//  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);//鼠标样式,NULL为从系统资源读取，hInstance为从本地资源读取
//	wc.hCursor		 = LoadCursorFromFile(TEXT("BullsEye.Cur"));//从文件读取
//  wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION);//图标样式,NULL为从系统资源读取，hInstance为从本地资源读取
//	wc.hIcon         = LoadIcon(hInstance,MAKEINTRESOURCE(ICON1));//从本地加载资源
//	wc.hIcon         = (HICON)LoadImage(NULL,TEXT("amcap.ico"),IMAGE_ICON,0,0,LR_SHARED|LR_LOADFROMFILE);//从文件读取
//注册窗口
    if(!RegisterClass(&wc)){
        //Msg(TEXT("RegisterClass Failed! Error=0x%x\r\n"), GetLastError());
		printf("RegisterClass Failed! Error=0x%x\r\n",GetLastError());
        return -1;
    }
	return 0;
}
HWND CreateWinByClassName(const char*classname,int width,int height){
HWND	hWnd= CreateWindow(classname, //类名
						"HelloWorld",	//标题栏
                        WS_OVERLAPPEDWINDOW|WS_CAPTION|WS_CLIPCHILDREN| WS_CLIPSIBLINGS,//窗口风格,会与wc.style进行或操作
                        CW_USEDEFAULT, //起始x位置
						CW_USEDEFAULT, //起始y位置
                        width, //起始x大小
						height,//起始y大小,含标题栏
                        NULL,//父窗口句柄
						NULL, //窗口菜单句柄
						NULL, //应用实例句柄
						NULL); //创建窗口参数
RECT WindowRect;  
RECT ClientRect;  
GetWindowRect(hWnd, &WindowRect);  
GetClientRect(hWnd, &ClientRect);
printf("%ld %ld %ld %ld\n",ClientRect.left,ClientRect.right,ClientRect.top,ClientRect.bottom);
printf("%ld %ld %ld %ld\n",WindowRect.left,WindowRect.right,WindowRect.top,WindowRect.bottom);
WindowRect.right += (width - ClientRect.right);  
WindowRect.bottom += (height - ClientRect.bottom);  
MoveWindow(hWnd, WindowRect.left, WindowRect.top, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top,1);  
return hWnd;
}